<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>captchaPage</title>
    <style type="text/css">
        .flex-grid {
            display: flex;
            flex-flow: wrap;
        }

        .ver-layout-center {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .card-layout {
            width: 20%;
            background: lightgray;
            padding: 10px;
            margin: 10px 5px;
            border-radius: 10px;
            /*    */
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .card-layout > p {
            font-size: 14px;
            font-weight: bold;
        }

        .card-layout > button, input {
            margin-top: 10px;
        }

        .card-layout > img {
            width: 100px;
            height: 50px;
            background: aliceblue;
        }

        #mcode-view {
            color: white;
            background: #70a284;
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 13px;
        }

        #captcha-input {
            padding: 3px 6px;
        }

        #result-view {
            margin-top: 10px;
            background: #6b99ea;
            color: white;
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 13px;
        }
    </style>
</head>
<body>
<div class="ver-layout-center">
    <h3>验证码种类</h3>
    <div class="flex-grid" style="width: 100%;">
        <div class="card-layout">
            <p>普通验证码图片</p>
            <img src="http://localhost:8084/captcha/captchaImage?type=line">
        </div>
        <div class="card-layout">
            <p>扭曲干扰验证码</p>
            <img src="http://localhost:8084/captcha/captchaImage?type=Shear">
        </div>
        <div class="card-layout">
            <p>gif验证码图片</p>
            <img src="http://localhost:8084/captcha/captchaImage?type=gif">
        </div>
        <div class="card-layout">
            <p>圆形干扰验证码</p>
            <img src="http://localhost:8084/captcha/captchaImage?type=circle">
        </div>
    </div>
</div>
<div class="ver-layout-center">
    <div class="card-layout">
        <p>验证码校验</p>
        <div id="mcode-view"></div>
        <img id="captcha-img" onclick="getCaptchaImage()" style="margin: 10px">
        <input id="captcha-input" placeholder="请输入验证码">
        <button onclick="captchaVerify()">验证码校验</button>
        <div id="result-view"></div>
    </div>
</div>


</body>
</html>

<script>
let mCode = ''

function getCaptchaImage() {
  mCode = Math.ceil(Math.random() * 100000000) //生成一个随机数（防止缓存）
  let src = "http://localhost:8084/captcha/captchaImage?type=line&code=" + mCode
  document.getElementById("mcode-view").innerText = "mCode：" + mCode
  document.getElementById("captcha-img").src = src
}

function captchaVerify() {
  let captchaCode = document.getElementById("captcha-input").value;
  let url = "http://localhost:8084/captcha/verifyCaptcha?code=" + mCode + "&captcha=" + captchaCode
  //网络请求
  let xhr = new XMLHttpRequest();
  xhr.open('get', url)
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send();
  xhr.onload = function () {
    console.log("f1-onload=======", xhr.responseText)
    let {status, message, data} = JSON.parse(xhr.responseText)
    document.getElementById("result-view").innerText = "校验结果：" + message
  }
}
</script>
